/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-05-08 23:06:06
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-06-19 16:16:26
 * @FilePath: /lesson39/2.blockqueue/Task.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include <string>
#include<unordered_map>
#include"log.hpp"
#include"Init.hpp"

Init init;

using namespace std;

class Task
{
public:
    Task() {}
    Task(const int &sockfd, const string &clientip, const uint16_t &clientport)
        : sockfd_(sockfd), clientip_(clientip), clientport_(clientport)
    {
    }
    void Run()
    {
        char buffer[4096];
        ssize_t n = read(sockfd_, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0;
            // cout << "client say: " << buffer << endl;
            cout << "client key: " << buffer << endl;

            string echo_string =init.Translation(buffer);
            // echo_string += buffer;
            n=write(sockfd_, echo_string.c_str(), echo_string.size());
            if(n<0)
            {
                lg(Warming,"errno: %d , strerror: %s",errno,strerror(errno));
            }
        }
        else if (n == 0)
        {
            lg(Info, "%s : %d quit!close sockfd : %d ", clientip_.c_str(), clientport_, sockfd_);
        }
        else
        {
            lg(Warming, "read error ,sockfd: %d ,clientip: %s ,clientport: %d", sockfd_, clientip_.c_str(), clientport_);
        }
        close(sockfd_);
    }
    void operator()()
    {
        Run();
    }
    ~Task() {}

private:
    int sockfd_;
    string clientip_;
    uint16_t clientport_;
};